import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 黎鹤舞
 * Date: 2024-04-15
 * Time: 20:34
 */
public class ArrayListTest {
    public ArrayList<Integer> intersection (ArrayList<Integer> nums1, ArrayList<Integer> nums2) {
        //先实例化一个顺序表对象:
        ArrayList<Integer> commonElements = new ArrayList<>();
        //简化两个顺序表
        nums1 = deleteCommon(nums1);
        nums2 = deleteCommon(nums2);
        for (int i = 0; i < nums1.size(); i++) {
            for (int j = 0; j < nums2.size(); j++) {
                if(nums1.get(i) == nums2.get(j)) {
                    commonElements.add(nums2.get(j));
                    break;
                }
            }
        }
        return commonElements;
    }

    public ArrayList<Integer> deleteCommon (ArrayList<Integer> nums) {
        ArrayList<Integer> simplify = new ArrayList<>();//记录简化后的顺序表
        for (int i = 0; i < nums.size(); i++) {
            int num = nums.get(i);
            if(simplify.indexOf(num) == -1) {   //如果在简化后的数组中未找到该数
                simplify.add(num);
            }
        }
        return simplify;
    }
}
